package bbs.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * 设置 拦截器
 * 注意 拦截器只对Controller中的url有效，对 静态资源无效
 * @author ljg;
 * @create 2022-06-09 8:15
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果没有登录就回到登录页面
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (Objects.isNull(user)){
            //此时表示 没有登录
            response.sendRedirect(request.getContextPath() + "/jsp/login.jsp");
            return false ;
        }
        return true ;
    }
}
